dojo.require("dojo.string");
//dojo.require("junc.log");
dojo.require("junc.log.Log");
//dojo.require("junc.ajax");
dojo.require("junc.ajax.Sro");
dojo.require("junc.dom.Behaviour");
dojo.require("junc.dom.Bind");
//dojo.require("junc.widget");
dojo.require("junc.widget.Page");


junc.log.setDebugEnable(false);
var log = new junc.log.Log("moo.adminCategory");
var domBind = new junc.dom.Bind();

var sro_category = null;
var pagination = null;
var rules = {
	'body': function(element) {
		sro_category = new junc.ajax.Sro(ajaxLoc);
		Admin.ADD_CATEGORY = categorAddLoc;
		Admin.PAGINATE_CATEGORY = categoryListLoc;
		//log.debug(junc.lang.describe(junc.widget));
		//log.debug(junc.widget.getGif("arrow_back"));
		pagination = new junc.widget.Page("CategoryPage.page");
		CategoryPage.page();	
	},
	
	'#btn-addCg' : function(element){
		element.onclick = function(){
			Admin.addCategory();
		}
	},
	
	'#menu_header1' : function(element) {
		element._opened = true;		
		element.onclick = function() {
			if(this._opened) {
				Effect.SlideUp('menu_block1');
				this._opened = false; 
			} else {
				Effect.SlideDown('menu_block1');
				this._opened = true; 
			}
		}
	},
	
	'#menu_header2' : function(element) {
		element._opened = true;		
		element.onclick = function() {
			if(this._opened) {
				Effect.SlideUp('menu_block2');
				this._opened = false; 
			} else {
				Effect.SlideDown('menu_block2');
				this._opened = true; 
			}
		}
	},
	
	'#menu_header3' : function(element) {
		element._opened = true;		
		element.onclick = function() {
			if(this._opened) {
				Effect.SlideUp('menu_block3');
				this._opened = false; 
			} else {
				Effect.SlideDown('menu_block3');
				this._opened = true; 
			}
		}
	},
	
	'#menu_header4' : function(element) {
		element._opened = true;
		//TODO bug: can't callback when this effect complete  
		element._moving = false;		
		element.onclick = function() {
			if(!this._moving) {
				this._moving = true;
				if(this._opened) {
					Effect.SlideUp('menu_block4');
					this._opened = false; 
					this._moving = false;
				} else {
					Effect.SlideDown('menu_block4');
					this._opened = true; 
					this._moving = false;
				}
			}
		}
	}		
};
junc.dom.Behaviour.register(rules);
// }}

function Admin(){};
Admin.ADD_CATEGORY = null;
Admin.PAGINATE_CATEGORY = null;
Admin.cleanErr = function(el) {
		el.style.cssText="";
		el.innerHTML="";
};
Admin.showErr = function(el, mess) {
		el.style.cssText="background-color:#ffdb9c;";
		el.innerHTML = mess;
};
Admin.showOk = function(el, mess) {
		el.style.cssText="background-color:#A4FFA4;";
		el.innerHTML = mess;
}
Admin.proveIntegrity = function(cg) {
		var errName = $("td-name-err");
		Admin.cleanErr(errName);
		var blank = "";
		if(blank == cg.name){
			Admin.showErr(errName, "名称不能为空");
			return false;
		};
		var len = cg.name.length;
		if(1 >  len  || 32 <  len) {
			Admin.showErr(errName, "名称长度不符合");
			return false;
		} 
		len = cg.description.lenght;
		if( len > 128) {
			Admin.showErr(errName, "描述长度不符合");
			return false;
		}
		return true;
}
Admin.addCategory = function() {
 	var result  = $("td-add-ok");	 	
	Admin.cleanErr(result);	 	 
 	var addCg = new Category( $("in-cgName").value, $("in-cgDes").value);
 	if (!Admin.proveIntegrity(addCg)) {
 		return;
 	}
	sro_category.remoteCall(Admin.ADD_CATEGORY, [addCg], function(reply) {
		var rs = reply.getResult();
		if(null === rs) {
			Admin.showOk(result, "添加成功");
			//repaginate
			CategoryPage.page();
		}else{
			Admin.showErr(result, "添加失败:<br/>" + rs.message);
		}
	}); 
};



var CategoryPage = {};
CategoryPage.page = function(pageId) {
	if(pageId) {
		CategoryPage._nowPageId = pageId;
	}
	var pageSize = 10;
	var startIndex = (CategoryPage._nowPageId - 1) * pageSize;
	sro_category.remoteCall(Admin.PAGINATE_CATEGORY, [startIndex, pageSize], function(reply) {
		//alert(reply.getSource());
		var rs = reply.getResult();
		domBind.bind("category-page-index", pagination.createIndex(rs.sum, CategoryPage._nowPageId, pageSize));
		CategoryPage._rowId = pagination.getRowStartIndex();
		domBind.bind("create-category-table", rs.page, {cellFuncs:CategoryPage._cellFuncs, cellCreator:CategoryPage._cellCreator});
	}); 
};
CategoryPage._rowId = 1;
CategoryPage._nowPageId = 1;
CategoryPage._rowId = 1;
CategoryPage._cellFuncs =  [
		  	function(data) { return CategoryPage._rowId ++; },
		  	function(data) { return data.name; },
		  	function(data) { return data.description ? data.description : ""; }];
CategoryPage._cellCreator =  { 
	rowCreator:function(options) {
    	var row = document.createElement("tr");
    	if (0 == options.rowIndex % 2) {
    		row.style.backgroundColor = "rgb(255, 255, 255)";
   		}     	
    	return row;
	},
 	cellCreator:function(options) {
    	var td = document.createElement("td");
    	if (0 == options.cellNum) {
    		td.style.backgroundColor = "rgb(166, 202, 240)";
    	}
    	return td;
  	}
};
//}}
// {{ value object classes
var Category = Class.create();
Category.prototype = {
	initialize: function(name, description){
		this.name = dojo.string.trim(name);
		this.description = description;
	}
}
//}}

